package com.banmao.mall.service.client.impl;

import com.banmao.mall.mapper.CategoryMapper;
import com.banmao.mall.model.pojo.Category;
import com.banmao.mall.model.vo.client.catalog.CategoryIndexVO;
import com.banmao.mall.model.vo.client.catalog.CurrentVO;
import com.banmao.mall.service.client.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    CategoryMapper categoryMapper;

    @Value("${myimage.baseURL}")
    private String imageURL;

    @Override
    public CategoryIndexVO index() {
        /**
         * 1.根据级别从数据库取出一级类目
         * 2.将第一个一级类目作为currentCategory
         * 3.根据currentCategory的id从数据库取出其子类目
         *
         * 注意：已逻辑删除的类目不取
         */
        CategoryIndexVO indexVO = new CategoryIndexVO();
        List<Category> categoryList = categoryMapper.selectCategoryByLevel("L1");
        //修改数据中本地上传图片的前缀
        judgePicFormat(categoryList);
        Category currentCategory = categoryList.get(0);
        List<Category> currentSubCategory = categoryMapper.selectCategoryByPID(currentCategory.getId());
        //修改数据中本地上传图片的前缀
        judgePicFormat(currentSubCategory);
        indexVO.setCategoryList(categoryList);
        indexVO.setCurrentCategory(currentCategory);
        indexVO.setCurrentSubCategory(currentSubCategory);
        return indexVO;
    }

    @Override
    public CurrentVO current(Integer id) {
        CurrentVO currentVO = new CurrentVO();
        Category currentCategory = categoryMapper.selectByPrimaryKey(id);
        //修改数据中本地上传图片的前缀
        if (currentCategory.getIconUrl() != null) {
            if (!currentCategory.getIconUrl().startsWith("http")) {
                currentCategory.setIconUrl(imageURL+currentCategory.getIconUrl());
            }
        }
        if (currentCategory.getPicUrl() != null) {
            if (!currentCategory.getPicUrl().startsWith("http")) {
                currentCategory.setPicUrl(imageURL+currentCategory.getPicUrl());
            }
        }
        List<Category> currentSubCategory = categoryMapper.selectCategoryByPID(id);
        //修改数据中本地上传图片的前缀
        judgePicFormat(currentSubCategory);
        currentVO.setCurrentCategory(currentCategory);
        currentVO.setCurrentSubCategory(currentSubCategory);
        return currentVO;
    }

    private void judgePicFormat(List<Category> list) {
        //修改数据中本地上传图片的前缀
        for (Category category : list) {
            if (category.getIconUrl() != null) {
                if (!category.getIconUrl().startsWith("http")) {
                    category.setIconUrl(imageURL+category.getIconUrl());
                }
            }
            if (category.getPicUrl() != null) {
                if (!category.getPicUrl().startsWith("http")) {
                    category.setPicUrl(imageURL+category.getPicUrl());
                }
            }
        }
    }
}
